package hammersmith.local.management;

import hammersmith.local.exceptions.EmptyCartException;
import hammersmith.local.exceptions.StockTooLowException;
import hammersmith.local.exceptions.WrongMinCountException;
import hammersmith.local.valueobjects.Article;
import hammersmith.local.valueobjects.Customer;
import hammersmith.local.valueobjects.Invoice;
import hammersmith.local.valueobjects.Log;
import hammersmith.local.valueobjects.MassArticle;
import hammersmith.local.valueobjects.Person;
import hammersmith.local.valueobjects.ShoppingCart;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Hashtable;

public class ShopServices implements Serializable {

	private static final long serialVersionUID = -951105337433606883L;

	public ShopServices() {

	}

	public void addToCart(Customer c, Article a, int count)
			throws StockTooLowException, WrongMinCountException {
		ShoppingCart cart = c.getCart();

		if (a instanceof MassArticle) {
			int minCount = ((MassArticle) a).getMinCount();
			cart.putArticleInCart(a, c, minCount);

		} else {
			cart.putArticleInCart(a, c, count);
		}

	}

	/**
	 * Methode zum Bezahlen (zur Kasse gehen)
	 * 
	 * @param shop
	 * @param handler
	 * @return
	 */
	public Object[] buy(Customer c, ArrayList<Customer> customerList,
			LogManagement logger) throws EmptyCartException {

		ArrayList<Log> logList = new ArrayList<Log>();
		Object[] objList = new Object[2];
		if (!c.getCart().isFilled()) {
			throw new EmptyCartException();
		}

		Invoice invoice;
		try {

			// aktuellen Zeitstempel errechnen
			Timestamp timestamp = new Timestamp(System.currentTimeMillis());

			// Durchlaufen des Warenkorbs...
			for (Article article : c.getCart().getList().keySet()) {

				int count = c.getCart().getList().get(article).intValue();

				// Log
				Log log = new Log((Person) c, article, timestamp.toString(),
						count, "gekauft");

				logList.add(log);

				// Bestand heruntersetzen...
				article.setStock(article.getStock() - count);
			}

			// Rechnung erstellen
			invoice = new Invoice(customerList, timestamp.toString(),
					c.getCustomerId(), c.getCart());

			c.getInvoiceList().add(invoice);

			objList[0] = logList;
			objList[1] = invoice;

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

		return objList;
	}

	public Hashtable<String, ArrayList<String>> getAllArticlesByType(
			ArrayList<Article> articleList, String input) {
		ArrayList<Article> onlyArticlesByTypeList = new ArrayList<Article>();
		ArrayList<String> nameList = new ArrayList<String>();
		// ArrayList<String> sizeList = new ArrayList<String>();

		Hashtable<String, ArrayList<String>> hash = new Hashtable<String, ArrayList<String>>();

		for (Article article : articleList) {
			if (article.getType().equals(input)) {

				if (!nameList.contains(article.getName())) {
					nameList.add(article.getName());
				}
				onlyArticlesByTypeList.add(article);
			}

			for (String string : nameList) {

				ArrayList<String> liste = new ArrayList<String>();

				for (Article a : articleList) {
					if (string.equals(a.getName())) {

						if (!liste.contains(a.getSize())) {
							liste.add(a.getSize());
						}
					}
				}
				hash.put(string, liste);
			}
		}
		return hash;
	}

	public String createNews(String txt) {
		char[] array = txt.toCharArray();
		String formatedText = null;
		int count = 0;
		for (int i = 0; i < array.length; i++) {
			if (count >= 70 && array[i] == ' ') {
				formatedText += "<br>";
				count = 0;
			}
			formatedText += array[i];
			count++;
		}
		return "<html>" + formatedText + "</html>";
	}

	public ArrayList<String> getAllColorsBySizeAndName(ArrayList<Article> list,
			String size, String name) {
		ArrayList<String> colorList = new ArrayList<String>();

		for (Article a : list) {
			if (a.getSize().equals(size) && a.getName().equals(name)) {
				if (!colorList.contains(a.getColor())) {
					colorList.add(a.getColor());
				}
			}
		}
		return colorList;
	}
}
